package com.xyht.sca_s.student_manage_system.modules.welcomeNewStudent.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.common.util.CacheUtil;
import com.xyht.sca_s.student_manage_system.common.util.TimeUtil;
import com.xyht.sca_s.student_manage_system.modules.appDynamic.entity.resp.UserInfoResp;
import com.xyht.sca_s.student_manage_system.modules.user.entity.SmsUser;
import com.xyht.sca_s.student_manage_system.modules.user.mapper.SmsUserMapper;
import com.xyht.sca_s.student_manage_system.modules.welcomeNewStudent.entity.SmsWnsEnrolNotice;
import com.xyht.sca_s.student_manage_system.modules.welcomeNewStudent.entity.req.SmsWnsEnrolNoticeReq;
import com.xyht.sca_s.student_manage_system.modules.welcomeNewStudent.entity.resp.SmsWnsEnrolNoticeResp;
import com.xyht.sca_s.student_manage_system.modules.welcomeNewStudent.mapper.SmsWnsEnrolNoticeMapper;
import com.xyht.sca_s.student_manage_system.modules.welcomeNewStudent.service.SmsWnsEnrolNoticeService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import static com.xyht.sca_s.student_manage_system.common.util.ParamCheckUtil.isNullOrEmpty;

/**
 * <p>
 * 入学须知表 服务实现类
 * </p>
 *
 * @author XYHT
 * @since 2024-01-25
 */
@Service
public class SmsWnsEnrolNoticeServiceImpl extends ServiceImpl<SmsWnsEnrolNoticeMapper, SmsWnsEnrolNotice> implements SmsWnsEnrolNoticeService {

    @Resource
    private SmsWnsEnrolNoticeMapper smsWnsEnrolNoticeMapper;
    @Resource
    private SmsUserMapper userMapper;
    @Resource
    private CacheUtil cacheUtil;

    @Override
    public ResponseResult addEnrolNotice(String userId, SmsWnsEnrolNoticeReq addReq) {
        if (isNullOrEmpty(addReq.getTheme(),addReq.getProfile(),addReq.getContent())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        SmsWnsEnrolNotice smsWnsEnrolNotice = new SmsWnsEnrolNotice();
        BeanUtils.copyProperties(addReq,smsWnsEnrolNotice);
        smsWnsEnrolNotice.setPublisher(userId);
        smsWnsEnrolNotice.setPublishTime(new Date());
        smsWnsEnrolNoticeMapper.insert(smsWnsEnrolNotice);
        return CommonResult.success();
    }

    @Override
    public ResponseResult deletedEnrolNotice(SmsWnsEnrolNoticeReq deletedReq) {
        if (isNullOrEmpty(deletedReq.getId())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        SmsWnsEnrolNotice smsWnsEnrolNotice = smsWnsEnrolNoticeMapper.selectById(deletedReq.getId());
        if (isNullOrEmpty(smsWnsEnrolNotice)) {
            return CommonResult.failed(CommonCodeEnum.ENROL_NOTICE_NOT_EXIST);
        }
        smsWnsEnrolNoticeMapper.deleteById(deletedReq.getId());
        return CommonResult.success();
    }

    @Override
    public ResponseResult updateEnrolNotice(SmsWnsEnrolNoticeReq updateReq) {
        if (isNullOrEmpty(updateReq.getId(),updateReq.getTheme(),updateReq.getProfile(),updateReq.getContent(),updateReq.getIsPost())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        SmsWnsEnrolNotice smsWnsEnrolNotice = smsWnsEnrolNoticeMapper.selectById(updateReq.getId());
        if (isNullOrEmpty(smsWnsEnrolNotice)) {
            return CommonResult.failed(CommonCodeEnum.ENROL_NOTICE_NOT_EXIST);
        }

        BeanUtils.copyProperties(updateReq,smsWnsEnrolNotice);
        smsWnsEnrolNoticeMapper.updateById(smsWnsEnrolNotice);
        return CommonResult.success();
    }

    @Override
    public ResponseResult getEnrolNoticeDetail(String id) {
        if (isNullOrEmpty(id)) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        SmsWnsEnrolNotice smsWnsEnrolNotice = smsWnsEnrolNoticeMapper.selectById(id);
        if (isNullOrEmpty(smsWnsEnrolNotice)) {
            return CommonResult.failed(CommonCodeEnum.ENROL_NOTICE_NOT_EXIST);
        }
        SmsWnsEnrolNoticeResp smsWnsEnrolNoticeResp = new SmsWnsEnrolNoticeResp();
        UserInfoResp userInfoResp = new UserInfoResp();
        BeanUtils.copyProperties(smsWnsEnrolNotice,smsWnsEnrolNoticeResp);
        SmsUser userInfo = cacheUtil.getUserInfo(smsWnsEnrolNotice.getPublisher());
        if (!isNullOrEmpty(userInfo)) {
           BeanUtils.copyProperties(userInfo,userInfoResp);
        }
        smsWnsEnrolNoticeResp.setPublisherInfo(userInfoResp);
        smsWnsEnrolNoticeResp.setPublishTime(TimeUtil.DateFormatSting(smsWnsEnrolNotice.getPublishTime()));
        return CommonResult.success(smsWnsEnrolNoticeResp);
    }

    @Override
    public ResponseResult getEnrolNoticeList(String publishTime, String theme, String publisher, Integer isPost, Integer pageNum, Integer pageSize) {
        Page<SmsWnsEnrolNotice> page = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<SmsWnsEnrolNotice> enrolNoticeLambdaQueryWrapper = new LambdaQueryWrapper<SmsWnsEnrolNotice>()
                .orderByDesc(SmsWnsEnrolNotice::getPublishTime);
        if (!isNullOrEmpty(publishTime)) {
            enrolNoticeLambdaQueryWrapper.eq(SmsWnsEnrolNotice::getPublishTime,publishTime);
        }
        if (!isNullOrEmpty(theme)) {
            enrolNoticeLambdaQueryWrapper.like(SmsWnsEnrolNotice::getTheme,theme);
        }
        if (!isNullOrEmpty(publisher)) {
            List<String> userIdList = userMapper.selectList(new LambdaQueryWrapper<SmsUser>()
                    .like(SmsUser::getUsername, publisher))
                    .stream().map(SmsUser::getId).collect(Collectors.toList());
            if (!userIdList.isEmpty()) {
                enrolNoticeLambdaQueryWrapper.in(SmsWnsEnrolNotice::getPublisher,userIdList);
            } else {
                enrolNoticeLambdaQueryWrapper.in(SmsWnsEnrolNotice::getPublisher,"");
            }
        }
        if (!isNullOrEmpty(isPost)) {
            enrolNoticeLambdaQueryWrapper.eq(SmsWnsEnrolNotice::getIsPost,isPost);
        }
        smsWnsEnrolNoticeMapper.selectPage(page,enrolNoticeLambdaQueryWrapper);

        List<SmsWnsEnrolNoticeResp> enrolNoticeList = page.getRecords()
                .stream()
                .map(smsWnsEnrolNotice -> {
                    SmsWnsEnrolNoticeResp smsWnsEnrolNoticeResp = new SmsWnsEnrolNoticeResp();
                    BeanUtils.copyProperties(smsWnsEnrolNotice,smsWnsEnrolNoticeResp);
                    UserInfoResp userInfoResp = new UserInfoResp();
                    BeanUtils.copyProperties(smsWnsEnrolNotice,smsWnsEnrolNoticeResp);
                    SmsUser userInfo = cacheUtil.getUserInfo(smsWnsEnrolNotice.getPublisher());
                    if (!isNullOrEmpty(userInfo)) {
                        BeanUtils.copyProperties(userInfo,userInfoResp);
                    }
                    smsWnsEnrolNoticeResp.setPublisherInfo(userInfoResp);
                    smsWnsEnrolNoticeResp.setPublishTime(TimeUtil.DateFormatSting(smsWnsEnrolNotice.getPublishTime()));
                    return smsWnsEnrolNoticeResp;
                })
                .collect(Collectors.toList());
        return CommonResult.success(enrolNoticeList,(int)page.getTotal());
    }
}
